package ticket8088.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import ticket8088.entity.param.TicketAddParam;
import ticket8088.entity.po.Schedule;
import ticket8088.entity.po.Ticket;
import ticket8088.entity.vo.TicketVO;
import ticket8088.mapper.ScheduleMapper;
import ticket8088.mapper.TicketMapper;
import ticket8088.service.TicketService;

import javax.annotation.Resource;
import java.time.LocalDateTime;
import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author ttxxi
 * @since 2022-05-31
 */
@Service
@Slf4j
public class TicketServiceImpl extends ServiceImpl<TicketMapper, Ticket> implements TicketService {
    @Resource
    TicketMapper ticketMapper;
    @Resource
    ScheduleMapper scheduleMapper;

    @Override
    public Boolean addTicket(TicketAddParam param){
        Schedule schedule = scheduleMapper.selectById(param.getScheduleId());
        if (schedule.getRemain() > 0){
            schedule.setRemain(schedule.getRemain() - 1);
            scheduleMapper.updateById(schedule);
        }else {
            log.info("没有余票了 {}", schedule);
            return false;
        }
        Ticket ticket = new Ticket();
        ticket.setUserId(param.getUserId());
        ticket.setScheduleId(param.getScheduleId());
        ticket.setCreateTime(LocalDateTime.now());
        int insert = ticketMapper.insert(ticket);
        return  insert ==1;
    }

    @Override
    public List<TicketVO> selectList(Long userId) {
        return ticketMapper.selectTicketVOByUserId(userId);
    }

}
